package com.mediacloud.app.newsmodule.view.share;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mediacloud.app.newsmodule.R;
import com.mediacloud.app.newsmodule.utils.SBShareUtils;
import com.mediacloud.app.share.model.ShareGridItem;
import com.mediacloud.app.share.view.BadgeView;
import com.mediacloud.app.share.view.SharePopGridWindow;
import com.utils.DeviceUtils;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class SharePopGridWindowH extends Dialog {
    private Context mContext;
    public GridAdaptor adaptor;
    public GridAdaptor adaptorFunc;
    public View contentView;
    public RecyclerView shareGrid;
    public RecyclerView funcationGrid;
    public SharePopGridWindow.OnGridItemClickListener listener;
    public boolean clickDismiss;
    public static final String FuncType="func";

    public SharePopGridWindowH(Context context) {
        this(context, R.style.AppfacShareProgressDialogStyle);
    }

    public SharePopGridWindowH(Context context, int style) {
        super(context, style);
        this.clickDismiss = true;
        this.mContext = context;
        this.contentView = LayoutInflater.from(this.mContext).inflate(this.getResLayoutId(), (ViewGroup) null);
        this.setContentView(this.contentView);
        this.shareGrid = this.contentView.findViewById(R.id.shareGrid);
        this.funcationGrid = this.contentView.findViewById(R.id.funcationGrid);
        this.adaptor = new GridAdaptor();
        this.adaptorFunc = new GridAdaptor();
        this.shareGrid.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        this.funcationGrid.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        this.shareGrid.setAdapter(this.adaptor);
        this.funcationGrid.setAdapter(this.adaptorFunc);
        this.contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });


    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (this.getWindow() != null && this.getWindow().getAttributes() != null) {
            WindowManager.LayoutParams lp = this.getWindow().getAttributes();
            lp.width = -1;
            lp.height = -1;
            this.getWindow().setAttributes(lp);
            this.setTranslucentStatus();
            this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }

    }

    protected void setTranslucentStatus() {
        if (Build.VERSION.SDK_INT >= 21) {
            this.getWindow().addFlags(-2147483648);
            this.getWindow().setStatusBarColor(0);
        } else {
            this.getWindow().addFlags(67108864);
        }

    }

    protected void setFit(View contentView) {
        View parent;
        for (View view = contentView; view.getParent() != this.getWindow().getDecorView(); view = parent) {
            parent = (View) view.getParent();
            parent.setFitsSystemWindows(this.getFitSystemWindow());
        }

    }

    protected boolean getFitSystemWindow() {
        return false;
    }

    public int getResLayoutId() {
        return R.layout.share_pop_win_root_h;
    }

    public void show(Activity activity) {
        super.show();
    }

    public void setShareGridAdaptorData(List<ShareGridItem> data) {
        this.adaptor.offsetPosition = 0;
        // this.adaptor.gridItemList = data;
        this.adaptor.gridItemList.clear();
        this.adaptor.gridItemList.addAll(data);

        Iterator<ShareGridItem> iterator = this.adaptor.gridItemList.iterator();
        while (iterator.hasNext()) {
            ShareGridItem item = iterator.next();
            if (item.label.equals(SBShareUtils.SharePoster)) {
                // iterator.remove();
                if (!DeviceUtils.isCus(mContext)) {
                    this.adaptor.offsetPosition++;
                }
                break;
            }
        }
        this.adaptor.notifyDataSetChanged();
    }

    public void setFuncGridAdaptorData(List<ShareGridItem> data) {
        // this.adaptorFunc.gridItemList = data;
        this.adaptorFunc.gridItemList.clear();
        this.adaptorFunc.gridItemList.addAll(data);
        Iterator<ShareGridItem> iterator = this.adaptorFunc.gridItemList.iterator();
        while (iterator.hasNext()) {
            ShareGridItem item = iterator.next();
            if (item.label.equals("清屏")) {
                iterator.remove();
            }
        }
        this.adaptorFunc.notifyDataSetChanged();
    }

    public void setonFuncItemClick(SharePopGridWindow.OnGridItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnGridItemClickListener {
        void setShareOnItemClick(int var1);

        void setFuncOnitemClick(int var1);
    }

    public class GridAdaptor extends RecyclerView.Adapter {
        public List<ShareGridItem> gridItemList = new ArrayList<>();
        public int offsetPosition = 0;

        protected GridAdaptor() {
        }
        @Override
        public int getItemCount() {
            return this.gridItemList == null ? 0 : this.gridItemList.size() - offsetPosition;
        }

        public ShareGridItem getItem(int position) {
            return this.gridItemList == null ? null : (ShareGridItem) this.gridItemList.get(position);
        }

        @NonNull
        public androidx.recyclerview.widget.RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            GridAdaptor.ViewHolder viewHolder = new GridAdaptor.ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.sharegrid_itemmodule_h, (ViewGroup) null));
            return viewHolder;
        }
        @Override
        public void onBindViewHolder(@NonNull androidx.recyclerview.widget.RecyclerView.ViewHolder hd, int position) {
            GridAdaptor.ViewHolder holder = (GridAdaptor.ViewHolder) hd;
            hd.itemView.setTag(R.id.share_item_rcy, position);
            int finalPosition = position + offsetPosition;
            ShareGridItem item = getItem(finalPosition);
            holder.shareGridItemImg.setImageResource(item.resId);
            holder.shareGirdItemTxt.setText(item.label);
            if (DeviceUtils.isCus(mContext)) {
                if (item.label.equals(SBShareUtils.SharePoster)) {
                    holder.shareGirdItemTxt.setText("截屏分享");
                    holder.shareGridItemImg.setImageResource(R.drawable.video_cut_icon);
                }
            } else {
                if (item.label.equals(SBShareUtils.SharePoster)) {
                    holder.itemView.setVisibility(View.GONE);
                } else {
                    holder.itemView.setVisibility(View.VISIBLE);
                }
            }

            hd.itemView.setOnClickListener(v -> {
                try {
                    if (listener != null) {
                        if (item.type.equals(SharePopGridWindow.FuncType)) {
                            listener.setFuncOnitemClick(finalPosition);
                        } else {
                            listener.setShareOnItemClick(finalPosition);
                        }
                    }
                } catch (Exception var4) {
                    var4.printStackTrace();
                }
            });

        }

        class ViewHolder extends androidx.recyclerview.widget.RecyclerView.ViewHolder {
            ImageView shareGridItemImg;
            TextView shareGirdItemTxt;
            BadgeView badgeView;

            public ViewHolder(View itemView) {
                super(itemView);
                this.shareGridItemImg = (ImageView) itemView.findViewById(R.id.shareGridItemImg);
                this.shareGirdItemTxt = (TextView) itemView.findViewById(R.id.shareGirdItemTxt);
                this.badgeView = new BadgeView(mContext);
            }
        }
    }
}
